<template>
  <div class="container">
    <SubBread></SubBread>
    <SubFilter @filter-change='filterChange'></SubFilter>
    <!-- 主体区 -->
    <div class="goods-list">
      <!-- 排序组件 -->
      <SubSort @sort-change='sortChange'></SubSort>
      <!-- 展示 -->
      <ul>
        <li v-for='item in goodsList' :key='item.id'>
          <GoodsItem :goods='item'></GoodsItem>
        </li>
      </ul>
      <!-- 无限加载组件 -->
      <XtxInfiniteLoading :loading='loading' :finished='finished' @infinite='getData'></XtxInfiniteLoading>
    </div>
  </div>
</template>

<script>
import SubBread from './sub-bread.vue'
import SubFilter from './sub-filter.vue'
import SubSort from './sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import XtxInfiniteLoading from '@/components/library/xtx-infinite-loading.vue'
import { reqFindSubCategoryGoods } from '@/api/category'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem,
    XtxInfiniteLoading
  },
  setup () {
    // 是否加载中以及数据是否加载完毕
    const loading = ref(false)
    const finished = ref(false)
    const route = useRoute()
    // 参数
    let reqParams = {
      page: 1,
      pageSize: 20
    }
    // 商品列表
    const goodsList = ref([])
    // 请求数据
    const getData = () => {
      // console.log('jiazai')
      loading.value = true
      // 根据分类ID查询商品
      reqParams.categoryId = route.params.id
      reqFindSubCategoryGoods(reqParams).then(data => {
        // 有数据
        if (data.result.items.length) {
          goodsList.value.push(...data.result.items)
          // 下一页
          reqParams.page++
        } else {
          finished.value = true
        }
        loading.value = false
      })
    }
    // 监听二级分类ID
    watch(() => route.params.id, (newVal) => {
      if (newVal && route.path === `/category/sub/${newVal}`) {
        goodsList.value = [] // 下面无限加载组件顶上来 进入可视区
        reqParams = {
          page: 1,
          pageSize: 20
        }
        finished.value = false
      }
    })
    // 1 更改排序组件数据
    const sortChange = (sortParams) => {
      // console.log(sortParams)
      finished.value = false
      // 合并请求参数，要保留之前参数
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      goodsList.value = []
    }
    // 2 更改筛选组件数据
    const filterChange = (filterParams) => {
      // console.log(filterParams)
      finished.value = false
      // 合并请求参数，要保留之前参数
      reqParams = { ...reqParams, ...filterParams }
      reqParams.page = 1
      goodsList.value = []
    }
    return { getData, loading, finished, goodsList, sortChange, filterChange }
  }
}
</script>

<style scoped lang='less'>
  .goods-list {
    background: #fff;
    padding: 0 25px;
    margin-top: 25px;
    ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
  }
</style>
